import 'package:ccshop/eventbus/CartGoodEvent.dart';
import 'package:ccshop/eventbus/EventBus.dart';
import 'package:ccshop/ui/pages/RefreshViewTest.dart';
import 'package:ccshop/ui/widget/cart/CartListView.dart';
import 'package:ccshop/utils/Constants.dart';
import 'package:flutter/material.dart';

class CartPage extends StatefulWidget {
  @override
  _CartPageState createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("购物车"),
          centerTitle: true,
        ),
        body: Container(
          color: Color(0xfff5f5f5),
          child: Column(
            children: [
              CartListView(),
              Container(
                height: 80,
                width: double.infinity,
                decoration: BoxDecoration(color: Color(0xfff5f5f5), boxShadow: [
                  BoxShadow(
                      color: Colors.black,
                      offset: Offset(0.0, 15.0), //阴影xy轴偏移量
                      blurRadius: 15.0, //阴影模糊程度
                      spreadRadius: 1.0 //阴影扩散程度
                      )
                ]),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 10, top: 4, bottom: 4),
                      child: Text(
                        "自营满￥99免配送费￥3",
                        style: TextStyle(color: Color(0xFF757575)),
                      ),
                    ),
                    Container(
                      width: double.infinity,
                      height: 1,
                      color: Color(0xFFE0E0E0),
                    ),
                    _CartBottomPayView(),
                  ],
                ),
              )
            ],
          ),
        ));
  }
}

class _CartBottomPayView extends StatefulWidget {
  @override
  _CartBottomPayViewState createState() => _CartBottomPayViewState();
}

class _CartBottomPayViewState extends State<_CartBottomPayView> {
  bool _isAllSelect = false; // 是否全部选中
  double totalPrice = 0.00;
  int goodCount = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    bus.on(Constants.EVENT_ALL_SELECT_ITEM, (arg) {
      CartGoodEvent event = arg;
      setState(() {
        _isAllSelect = event.isAllSelectItem;
        totalPrice = double.parse(event.totalPrice.toStringAsFixed(2));
        goodCount = event.goodCount;
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    bus.off(Constants.EVENT_ALL_SELECT_ITEM);
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            children: [
              Padding(
                padding: EdgeInsets.only(left: 10),
              ),
              InkWell(
                onTap: () {
                  setState(() {
                    _isAllSelect = !_isAllSelect;
                    bus.emit(Constants.EVENT_ALL_SELECT, _isAllSelect);
                  });
                },
                child: Icon(
                  Icons.check_circle,
                  color: _isAllSelect ? Colors.red : Colors.grey,
                ),
              ),
              Padding(
                padding: EdgeInsets.only(left: 6),
              ),
              Text("全选")
            ],
          ),
          Row(
            children: [
              Text("合计:"),
              Text(
                "￥$totalPrice",
                style: TextStyle(
                    color: Colors.red,
                    fontWeight: FontWeight.bold,
                    fontSize: 18),
              ),
              Padding(
                padding: EdgeInsets.only(right: 10),
              ),
              InkWell(
                onTap: () {
                  Navigator.push(context, MaterialPageRoute(builder:(context){
                    return RefreshViewTest();
                  }));
                },
                child: Container(
                  height: double.infinity,
                  color: Colors.red,
                  width: 100,
                  alignment: Alignment.center,
                  child: Text(
                    "去结算($goodCount)",
                    style: TextStyle(
                        color: Colors.white,
                        fontSize: 16,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              )
            ],
          )
        ],
      ),
    );
  }
}
